<?php

namespace App\Admin\Controllers\Chart;

use App\Models\Order;
use App\Models\Scale;
use Carbon\Carbon;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Rap2hpoutre\FastExcel\Facades\FastExcel;

class RoomChartController
{
    public function index(Content $content)
    {
    	// x 轴为房型
    	$data = [
			'oneDay' => $this->Chart('day'),
			'oneMonth' => $this->Chart('month'),
			'oneYear' => $this->Chart('year')
		];
    	
        return $content
	        ->title('房型统计')
	        ->header('房型统计')
	        //->row(view('admin.export.export-btn', ['route' => route('roomExport')]))
	        ->row(function (Row $row) use ($data){
        
				    $row->column(10, function (Column $column) use($data) {
				    	$column->row(new Box('今日', view('admin.chart.room-chart', ['name' => 'oneDay', 'data' => $data['oneDay']])));
				    	$column->row(new Box('本月', view('admin.chart.room-chart', ['name' => 'oneMonth', 'data' => $data['oneMonth']])));
				        $column->row(new Box('本年', view('admin.chart.room-chart', ['name' => 'oneYear', 'data' => $data['oneYear']])));
				    });
				});
    }
    
    public function Chart($index = 'day')
    {
		$timeStart = new Carbon();
    	$timeEnd = new Carbon();
    	$timeBetween = [];
	    switch ($index) {
	    	case 'day':
	    		$timeBetween = [$timeStart->startOfDay(), $timeEnd->endOfDay()];
	    		break;
	    	case 'month':
	    		$timeBetween = [$timeStart->startOfMonth(), $timeEnd->endOfMonth()];
	    		break;
	    	case 'year':
	    		$timeBetween = [$timeStart->startOfYear(), $timeEnd->endOfYear()];
	    		break;
	    	
	    	default:
	    		$timeBetween = [$timeStart->startOfYear(), $timeEnd->endOfYear()];
	    		break;
	    }
        // x 轴为房型
	    
    	$data = Order::where('store_id', Admin::user()->store_id)
		    ->whereBetween('created_at', $timeBetween)
		    ->selectRaw('room_id, count(*) as value')
		    ->with('room')
		    ->groupBy('room_id')
		    ->get();
		$data = $data->groupBy('room.scale_id');
    	$scale = Scale::where('store_id', Admin::user()->store_id?Admin::user()->store_id:1)
		    ->with('room')
	        ->get();
    	
    	foreach ($scale as $k => $v)
	    {
			if(isset($data[$v['id']])){
				$number = $data[$v['id']]->sum('value');
			} else {
				$number = 0;
			}
			$y[$v['id']] = [
				'x' => $v['name'],
				'value' => $number
			];
	    
	    }
		
    	return $y;
    }
    // 导出
    public function export(Request $request)
    {
    	$time = new Carbon();
    	switch($request->get('date'))
	    {
		    case 'year':
		    	return FastExcel::data($this->exportData('year'))
	        ->download('订单.'.$time->format('Y').'年.xlsx');
		    	break;
		    case 'month':
		    	return FastExcel::data($this->exportData('month'))
	            ->download('订单.'.$time->format('Y-M').'月.xlsx');
		    	break;
		    case 'day':
		    	return FastExcel::data($this->exportData('day'))
	            ->download('订单.'.$time->format('Y-m-d').'.xlsx');
		    	break;
	    }
	    
    }
    
    public function exportData($index = 'year')
    {
    	$timeStart = new Carbon();
    	$timeEnd = new Carbon();
    	$timeBetween = [];
    	
    	$scale = Scale::get(['id', 'name']);
	    switch ($index) {
	    	case 'day':
	    		$timeBetween = [$timeStart->startOfDay(), $timeEnd->endOfDay()];
	    		$data = Order::whereBetween('created_at', $timeBetween)
			    ->selectRaw('room_id, count(*) as value')
			    ->with('room')
			    ->groupBy('room_id')
			    ->get();
			    $data = $this->dataCollection($scale, $data->groupBy('room.scale_id'));
	    		break;
	    	case 'month':
	    		$timeBetween = [$timeStart->startOfMonth(), $timeEnd->endOfMonth()];
	    		$data = Order::whereBetween('created_at', $timeBetween)
			    ->selectRaw('room_id, count(*) as value')
			    ->with('room')
			    ->groupBy('room_id')
			    ->get();
			    $data = $this->dataCollection($scale, $data->groupBy('room.scale_id'));
	    		break;
	    	case 'year':
	    		$timeBetween = [$timeStart->startOfYear(), $timeEnd->endOfYear()];
	    		$data = Order::whereBetween('created_at', $timeBetween)
			    ->selectRaw('room_id, count(*) as value')
			    ->with('room')
			    ->groupBy('room_id')
			    ->get();
	    		//dd($scale->toArray(), $data->groupBy('room.scale_id'));
	    		$data = $this->dataCollection($scale, $data->groupBy('room.scale_id'));
	    		break;
	    	
	    	default:
	    		$timeBetween = [$timeStart->startOfYear(), $timeEnd->endOfYear()];
	    		break;
	    }

	    return collect($data);
    }
    
    public function dataCollection($scale,Collection $data)
    {
       // $data = collect([
       // 	'中房' => 10,
	      //   '大房' => 12,
	      //   '小房' => 5
       // ]);
        
        foreach ($scale as $v){
            static $arr = [];
            if(isset($data[$v['id']])){
                $number = $data[$v['id']]->sum('value');
            } else {
            	$number = 0;
            }
            $arr[$v['id']] = [
            	'name' => $v['name'],
	            'number' => $number
            ];
        }

        return $arr;
        //dd($arr,$data->toArray() ,$data[1]->sum('value'));
    }
}
